package com.ruoyi.demo.controller.User;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.demo.domain.UserPhoto;
import com.ruoyi.demo.service.ITakePhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/demo/photo")
public class TakePhotoController extends BaseController {
    
    @Autowired
    private ITakePhotoService ITakePhotoService;

    @PostMapping("/upload")
    public AjaxResult uploadPhoto(MultipartFile photo) {
        try {
            // 获取当前登录用户
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long userId = loginUser.getUserId();
            String userName = loginUser.getUsername();

            // 保存照片文件
            String photoPath = ITakePhotoService.savePhoto(photo, userId, userName);

            // 保存记录到数据库
            UserPhoto userPhoto = new UserPhoto();
            userPhoto.setUserId(userId);
            userPhoto.setPhotoPath(photoPath);
            ITakePhotoService.insertUserPhoto(userPhoto);

            return AjaxResult.success("照片上传成功");
        } catch (Exception e) {
            return AjaxResult.error("照片上传失败：" + e.getMessage());
        }
    }
}
